<?php

namespace app\lib\tool;

class Str
{
    /* 生成token */
    public static function getToken($string)
    {
        $str = md5(uniqid(md5(microtime(true)), true));
        $token = sha1($str . $string);
        return $token;
    }

    /**
     * 随机字符串
     * @param  [integer] $type   [类型]
     * @param  [integer] $length [长度]
     */
    public static function random($type, $length)
    {
        switch ($type) {
            case 1:
                $strPol = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
                break;

            case 2:
                $strPol = '0123456789';
                break;
        }

        $str = '';
        $max = strlen($strPol) - 1;
        for ($i = 0; $i < $length; $i++) {
            $str .= $strPol[rand(0, $max)];
        }
        return $str;
    }

    /* font标签转换span标签 */
    public static function fontToSpan($content)
    {
        preg_match_all('/<font .*?>.*?<\/font>/i', $content, $arr);
        foreach ($arr[0] as $v) {
            preg_match_all('/<font .*?>.*?/i', $v, $_arr);
            foreach ($_arr[0] as $j) {
                $basic_size = '14px';
                $basic_color = '#333';
                $basic_face = '微软雅黑';
                preg_match_all('/size="[^"]*?"/i', $v, $size);
                preg_match_all('/color="[^"]*?"/i', $v, $color);
                preg_match_all('/face="[^"]*?"/i', $v, $face);

                if (count($size[0])) {
                    $size = str_replace('"', '', $size[0][0]);
                    $size = str_replace('size=', '', $size);
                    switch ($size) {
                        case '1':
                            $basic_size = '10px';
                            break;
                        case '2':
                            $basic_size = '13px';
                            break;
                        case '3':
                            $basic_size = '16px';
                            break;
                        case '4':
                            $basic_size = '18px';
                            break;
                        case '5':
                            $basic_size = '24px';
                            break;
                        case '6':
                            $basic_size = '32px';
                            break;
                        case '7':
                            $basic_size = '48px';
                            break;
                    }
                }
                if (count($color[0])) {
                    $color = str_replace('"', '', $color[0][0]);
                    $basic_color = str_replace('color=', '', $color);
                }
                if (count($face[0])) {
                    $face = str_replace('"', '', $face[0][0]);
                    $basic_face = str_replace('face=', '', $face);
                }
                $str = "<span style=\"font-size:$basic_size;color:$basic_color;font-family:$basic_face;\">";
                $content = str_replace($j, $str, $content);
            }
        }
        $content = str_replace('</font>', '</span>', $content);
        return $content;
    }
}
